package 设计模式.singleton.test3;


public class SING1 {
    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        Singleton instance2 = Singleton.getInstance();
        System.out.println(instance==instance2);
        System.out.println("instance的hashcode="+instance.hashCode());
        System.out.println("instance的hashcode2="+instance2.hashCode());
    }
}

//静态内部类
class Singleton {
    private static volatile Singleton singleton;
    private Singleton() {
    }

    private static class SingletonInstance {
        private static final Singleton INSTANCE=new Singleton();
    }
    public static Singleton getInstance(){
        return SingletonInstance.INSTANCE;
    }
}

/*
class Singleton {
    //线程1、2进入getInstance()方法，读到内存中singleton==null，线程1创建对象，然后volatile告诉线程2内存更新对象
    private static volatile Singleton singleton;

    private Singleton() {
    }

    public static Singleton getInstance() {
        if (singleton == null) {
            synchronized (Singleton.class) {
                if (singleton == null){
                    singleton = new Singleton();
                }

            }

        }
        return singleton;
    }
}*/
